package com.woniu.p2p.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.p2p.entity.User;
import com.woniu.p2p.entity.UserZong;
import com.woniu.p2p.service.UserInfoSevice;
import com.woniu.p2p.vo.UserInfoVo;
import com.woniu.p2p.vo.UserInvestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * UserHomPageController
 *
 * @date 2020/8/7
 */
@RestController
@RequestMapping("/userHomePageController")
public class UserHomPageController {


    @Autowired
    UserInfoSevice userInfoSevice;

    /**
     * 查询个人信息
     *
     * @return
     */
    @RequestMapping("/getSelfInfo")
    @ResponseBody
    public UserInfoVo getSelfInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer i1 = Integer.parseInt(token);
        UserInfoVo byUserId = userInfoSevice.findByUserId(i1);
        byUserId.setLoansDoce(userInfoSevice.selectloansDoce(i1));
        byUserId.setLoansMoney(userInfoSevice.selectLonasMoney(i1));
        byUserId.setInvesMoney(userInfoSevice.selectUserPayMoney(i1));
        byUserId.setInvesDoce(userInfoSevice.selectUserInvest(i1));
        return byUserId;
    }

    /**
     * 查询用户资金记录
     *
     * @param request
     * @return
     */
    @RequestMapping("/getuserZong")
    @ResponseBody
    public PageInfo<UserZong> getuserZong(@RequestBody Map<String, Integer> map,HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer userId = Integer.parseInt(token);
        Integer pageNum = map.get("pageNum");
        PageInfo<UserZong> userZongPageInfo = userInfoSevice.seletAllUserZong(userId, pageNum, 3);
        return userZongPageInfo;
    }

    /**
     * 查询用户投资记录
     *
     * @return
     */
    @RequestMapping("/getUserInvestVo")
    @ResponseBody
    public PageInfo<UserInvestVo> getUserInvestVo(@RequestBody Map<String, Integer> map,HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer i1 = Integer.parseInt(token);
        Integer pageNum = map.get("pageNum");
        PageInfo<UserInvestVo> userInvestVoPageInfo = userInfoSevice.selectAllUserInvestVo(i1, pageNum, 3);
        return userInvestVoPageInfo;
    }

    @RequestMapping("/getUserInfoById")
    @ResponseBody
    public User getUserInfoById(HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer i1 = Integer.parseInt(token);
        return userInfoSevice.findUserInfoById(i1);
    }

    /**
     * 邮箱认证
     *
     * @param eamil
     * @param request
     * @return
     */
    @RequestMapping("/changeEmail/{eamil}")
    public int changEmail(@PathVariable("eamil") String eamil, HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer userid = Integer.parseInt(token);
        User user = userInfoSevice.findUserInfoById(userid);
        user.setUserEmail(eamil);
        return userInfoSevice.updateUser(user);
    }

    /**
     * 手机认证修改
     *
     * @param phone
     * @param request
     * @return
     */
    @RequestMapping("/changephone/{phone}")
    public int changePhone(@PathVariable("phone") String phone, HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer userid = Integer.parseInt(token);
        User user = userInfoSevice.findUserInfoById(userid);
        user.setUserPhone(phone);
        userInfoSevice.updateUser(user);
        return 1000;
    }

    /**
     * 修改登录密码
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping("/changPass")
    public int changPass(@RequestBody Map<String, String> map, HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer userid = Integer.parseInt(token);
        User user = userInfoSevice.findUserInfoById(userid);
        if (!map.get("oldpass").equals(user.getUserPassword())) {
            return 0;
        }
        user.setUserPassword(map.get("renewpass"));
        userInfoSevice.updateUser(user);
        return 1;
    }

    /**
     * 修改支付密码
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping("/changePaypass")
    public int changPaypass(@RequestBody Map<String, String> map, HttpServletRequest request) {
        System.out.println(map);
        String token = request.getHeader("token");
        Integer userid = Integer.parseInt(token);
        User user = userInfoSevice.findUserInfoById(userid);
        if (!map.get("oldpaypass").equals(user.getUserPaypwd())) {
            return 0;
        }
        user.setUserPaypwd(map.get("renewpaypass"));
        userInfoSevice.updateUser(user);
        return 1;
    }


    /**
     * 修改个人信息
     *
     * @param user
     * @return
     */
    @RequestMapping("/changUserInfo")
    @ResponseBody
    public int changUserInfo(@RequestBody User user) {
        System.out.println(user);
        userInfoSevice.updateUser(user);
        return 0;
    }

    /**
     * 身份认证
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping("/addUsrIDCard")
    public int addUsrIDCard(@RequestBody Map<String, String> map, HttpServletRequest request) {
        String card = map.get("card");
        String realName = map.get("realName");
        String token = request.getHeader("token");
        Integer userid = Integer.parseInt(token);
        User user = userInfoSevice.findUserInfoById(userid);
        user.setUserIdcard(card);
        user.setUserRealname(realName);
        userInfoSevice.updateUser(user);
        return 0;
    }
}
